/*
	File:		InputShip.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	CInputShip class handles the ship entity
*/
#include "InputShip.h"
#include <Windows.h>
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../SGD Wrappers/CSGD_XAudio2.h"
#include "../SGD Wrappers/CSGD_MessageSystem.h"

#include "Message.h"
#include "CreateLaserMessage.h"
#include "Game.h"
#include <cassert>

#define HALVE_PI 1.57079632f

CInputShip::CInputShip() : CShip()
{
	movementState = MOVE_INPUT;
	m_eType = ENT_INPUTSHIP;
}

CInputShip::~CInputShip()
{
	CShip::~CShip();
}

void CInputShip::Update( float fElapsedTime )
{
	PowerUp_Update(fElapsedTime);

	CSGD_DirectInput* keys = CSGD_DirectInput::GetInstance();
	if(keys->KeyPressed(DIK_0))
	{
		if(movementState==MOVE_INPUT)
			movementState = MOVE_AI;
		else if(movementState == MOVE_AI)
			movementState = MOVE_INPUT;
	}

	if(spinOutTimer>0)
	{
		CGame::GetInstance()->ChangeScore((int)(-1337*fElapsedTime));
		SpinOut(fElapsedTime);
	}
	else
	{	
		if(movementState==MOVE_INPUT)
			Input_Update(fElapsedTime);
		else if(movementState == MOVE_AI)
			AI_Update(fElapsedTime);
		ApplyDraggingEffect(fElapsedTime);
	}
	CShip::Update(fElapsedTime);
}

void CInputShip::Render( void )
{
	CGame* gamePtr = CGame::GetInstance();

	assert( GetImageID() != -1 && "CShip::Render - invalid image id!" );

	RECT srcRECT = {0,0,512,512};
	float scale = 512.0f / GetWidth();

	gamePtr->SetCameraX(int(GetPosX()-gamePtr->GetWidth()*0.5f));
	gamePtr->SetCameraY(int(GetPosY()-gamePtr->GetHeight()*0.5f));
	gamePtr->SetCameraRotation(-m_fCameraRot);

	CSGD_TextureManager::GetInstance()->Draw( GetImageID(),
		(int)GetPosX()-GetWidth()/2-gamePtr->GetCameraX(), (int)GetPosY()-GetHeight()/2-gamePtr->GetCameraY(),
		1.0f/scale,1.0f/scale,&srcRECT, 
		256,256,m_fRotation+gamePtr->GetCameraRotation());

	if(CGame::DEBUG_MODE)
	{
		RECT collisonRECT = GetRect();
		OffsetRect(&collisonRECT,-gamePtr->GetCameraX(),-gamePtr->GetCameraY());
		CSGD_Direct3D::GetInstance()->DrawRect(collisonRECT,255,255,0);
	}
}